﻿using APIShoppingSystem.Model;
using IShoppingBll;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ShoppingEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace APIShoppingSystem.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class CommodityTypeController : ControllerBase
    {
        private ICommodityTypeBll _commodityTypeBll;

        public CommodityTypeController(ICommodityTypeBll commodityTypeBll)
        {
            _commodityTypeBll = commodityTypeBll;
        }

        /// <summary>
        /// 类别表列表展示、分页模糊查询
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="categoryName"></param>
        /// <returns></returns>

        [HttpGet]
        public IActionResult GetCommodityTypePageList(int page, int limit, string CategoryName)
        {
            int count;

            object categorys = _commodityTypeBll.GetCommodityTypePageList(page, limit, out count, CategoryName);

            return new JsonResult(new
            {
                code = 0,
                msg = "成功",
                count = count,
                data = categorys
            });
        }


        /// <summary>
        /// 添加商品类别
        /// </summary>
        /// <param name="CategoryName"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddCommodityType([FromForm] string CategoryName)
        {
            CustomActionResult res = new CustomActionResult();

            if (string.IsNullOrEmpty(CategoryName))
            {
                res.Msg = "商品类别名称不能为空";
            }

            //判断是否有相同的商品类型名称
            if (_commodityTypeBll.IsHadCommodityType(CategoryName))
            {
                res.Msg = "当前添加的商品类别名称已存在";
            }
            else
            {
                CommodityType entity = new CommodityType()
                {
                    ID = Guid.NewGuid().ToString(),
                    CategoryName = CategoryName
                };
                bool isSuccess = _commodityTypeBll.Add(entity);
                if (isSuccess)
                {
                    res.IsSuccess = true;
                    res.Status = 1;
                    res.Msg = "添加成功";
                }
            }
            return new JsonResult(res);
        }


        /// <summary>
        /// 获取更新商品类别页面信息
        /// </summary>
        /// <param name="categoryId"></param> 
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetUpdateCommodityType(string CategoryId)
        {
            CustomActionResult res = new CustomActionResult();
            //获取商品类别信息
            CommodityType category = _commodityTypeBll.GetEntity(CategoryId);

            res.IsSuccess = true;
            res.Status = 1;
            res.Msg = "成功";
            res.Datas = new
            {
                Category = category
            };
            return new JsonResult(res);
        }

        /// <summary>
        /// 更新类别
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UpdateCommodityType([FromForm] string CategoryId, [FromForm] string CategoryName)
        {
            CustomActionResult res = new CustomActionResult();

            CommodityType entity = _commodityTypeBll.GetEntity(CategoryId);
            entity.CategoryName = CategoryName;

            if (_commodityTypeBll.UpdateEntity(entity))
            {
                res.IsSuccess = true;
                res.Msg = "成功";
                res.Status = 1;
                return new JsonResult(res);
            }
            else
            {
                return new JsonResult(res);
            }
        }


        /// <summary>
        /// 删除商品类别
        /// </summary>
        /// <param name="categoryId"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult DeleteCategory([FromForm] string CategoryId)
        {
            CustomActionResult res = new CustomActionResult();

            if (_commodityTypeBll.Delete(CategoryId))
            {
                res.Status = 1;
                res.Msg = "成功";
                res.IsSuccess = true;
                return new JsonResult(res);
            }
            else
            {
                return new JsonResult(res);
            }
        }
    }
}
